#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=..\icon.ico
#AutoIt3Wrapper_Compile_Both=y
#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_Res_Fileversion=0.0.1.0
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=p
#AutoIt3Wrapper_Res_Language=1040
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#AutoIt3Wrapper_Run_After=move "lockscreen.exe" "..\MLLockscreen.exe"
#AutoIt3Wrapper_Run_After=move "lockscreen_x64.exe" "..\MLLockscreen_x64.exe"
#AutoIt3Wrapper_Run_Tidy=y
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

#include "graphic.au3"
#include "common.au3"

Opt("GUIOnEventMode", 1)

If WinExists("Metro Launch") Then _LockScreen_Activate()

Func _LockScreen_Activate()
	_LoadTranslation(IniRead(@ScriptDir & "\settings.ini", "general", "lang", "English.lng"))
	_LoadSettings()
	Local $sBackground = $Settings_LS_Background
	Local $iDateScheme = $Settings_LS_DateScheme
	Local $aDayName[7] = [$aLang[27], $aLang[28], $aLang[29], $aLang[30], $aLang[31], $aLang[32], $aLang[33]]
	Local $aMonName[12] = [$aLang[34], $aLang[35], $aLang[36], $aLang[37], $aLang[38], $aLang[39], $aLang[40], $aLang[41], $aLang[42], $aLang[43], $aLang[44], $aLang[45]]
	Global $hLockGUI = GUICreate("Metro Launch Lockscreen", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, $WS_EX_TOOLWINDOW)
	If StringLeft($sBackground, 2) = "0x" Then
		GUISetBkColor($sBackground)
	ElseIf FileExists($sBackground) Then
		GUICtrlCreatePic($sBackground, 0, 0, @DesktopWidth, @DesktopHeight)
		GUICtrlSetState(-1, $GUI_DISABLE)
	Else
		GUISetBkColor(0x000000)
	EndIf
	WinSetOnTop($hLockGUI, "", 1)
	GUISetOnEvent($GUI_EVENT_CLOSE, "_LockScreen_Exit")
	Local $sTime = GUICtrlCreateLabel(@HOUR & ":" & @MIN, 100, @DesktopHeight / 8 * 5, @DesktopWidth, 180)
	GUICtrlSetFont(-1, 72, 200, 0, "Segoe UI Semilight")
	GUICtrlSetColor(-1, 0xffffff)
	GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
	GUICtrlSetOnEvent(-1, "_Drag")
	Switch $iDateScheme
		Case "0"
			Local $sDate = GUICtrlCreateLabel($aDayName[@WDAY - 1] & ", " & @MDAY & " " & $aMonName[@MON - 1] & " " & @YEAR, 100, @DesktopHeight / 8 * 5 + 120, @DesktopWidth, 180)
			GUICtrlSetFont(-1, 28, 200, 0, "Segoe UI Semilight")
			GUICtrlSetColor(-1, 0xffffff)
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
			GUICtrlSetOnEvent(-1, "_Drag")
		Case "1"
			Local $sDate = GUICtrlCreateLabel(@MDAY & " " & $aMonName[@MON - 1] & " " & @YEAR, 100, @DesktopHeight / 8 * 5 + 120, @DesktopWidth, 180)
			GUICtrlSetFont(-1, 28, 200, 0, "Segoe UI Semilight")
			GUICtrlSetColor(-1, 0xffffff)
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
			GUICtrlSetOnEvent(-1, "_Drag")
		Case "2"
			Local $sDate = GUICtrlCreateLabel(@MDAY & "/" & @MON & "/" & @YEAR, 100, @DesktopHeight / 8 * 5 + 120, @DesktopWidth, 180)
			GUICtrlSetFont(-1, 28, 200, 0, "Segoe UI Semilight")
			GUICtrlSetColor(-1, 0xffffff)
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
			GUICtrlSetOnEvent(-1, "_Drag")
		Case "3"
			Local $sDate = GUICtrlCreateLabel($aDayName[@WDAY - 1] & ", " & $aMonName[@MON - 1] & " " & @MDAY, 100, @DesktopHeight / 8 * 5 + 120, @DesktopWidth, 180)
			GUICtrlSetFont(-1, 28, 200, 0, "Segoe UI Semilight")
			GUICtrlSetColor(-1, 0xffffff)
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
			GUICtrlSetOnEvent(-1, "_Drag")
	EndSwitch
	Local $hClickZone = GUICtrlCreateLabel("", 0, 0, @DesktopWidth, @DesktopHeight)
	GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
	GUICtrlSetOnEvent(-1, "_Drag")
	GUIRegisterMsg($WM_WINDOWPOSCHANGING, "WM_WINDOWPOSCHANGING")
	GUISetState(@SW_SHOW)
	_GUI_MoveFull($hLockGUI, 1)

	While 1
		If GUICtrlRead($sTime) <> @HOUR & ":" & @MIN Then GUICtrlSetData($sTime, @HOUR & ":" & @MIN)
		Switch $iDateScheme
			Case "0"
				If GUICtrlRead($sDate) <> $aDayName[@WDAY - 1] & ", " & @MDAY & " " & $aMonName[@MON - 1] & " " & @YEAR Then GUICtrlSetData($sDate, $aDayName[@WDAY - 1] & ", " & @MDAY & " " & $aMonName[@MON - 1] & " " & @YEAR)
			Case "1"
				If GUICtrlRead($sDate) <> @MDAY & " " & $aMonName[@MON - 1] & " " & @YEAR Then GUICtrlSetData($sDate, @MDAY & " " & $aMonName[@MON - 1] & " " & @YEAR)
			Case "2"
				If GUICtrlRead($sDate) <> @MDAY & "/" & @MON & "/" & @YEAR Then GUICtrlSetData($sDate, @MDAY & "/" & @MON & "/" & @YEAR)
			Case "3"
				If GUICtrlRead($sDate) <> $aDayName[@WDAY - 1] & ", " & $aMonName[@MON - 1] & " " & @MDAY Then GUICtrlSetData($sDate, $aDayName[@WDAY - 1] & ", " & $aMonName[@MON - 1] & " " & @MDAY)
		EndSwitch
		If Not WinActive($hLockGUI) Then WinActivate($hLockGUI)
	WEnd
EndFunc   ;==>_LockScreen_Activate

Func _Drag()
	AdlibRegister("_CDrag", 10)
	Global $CInfo = GUIGetCursorInfo($hLockGUI)
EndFunc   ;==>_Drag

Func _LockScreen_Exit()
	Local $aWinPos = WinGetPos($hLockGUI)
	For $i = 0 To -(@DesktopHeight - $aWinPos[3] / 700) Step -30
		WinMove($hLockGUI, "", 0, $i)
		Sleep(10)
	Next
	For $i2 = -(@DesktopHeight - $aWinPos[3] / 700) To -@DesktopHeight Step -10
		WinMove($hLockGUI, "", 0, $i2)
		Sleep(10)
	Next
	Exit
EndFunc   ;==>_LockScreen_Exit

Func _CDrag()
	$aWinPos = WinGetPos($hLockGUI)
	$MCInfo = GUIGetCursorInfo($hLockGUI)
	If Not $MCInfo[2] Then
		AdlibUnRegister("_CDrag")
		If $aWinPos[1] <= -@DesktopHeight / 4 Then
			For $i = $aWinPos[1] To -@DesktopHeight Step -((@DesktopHeight - ($aWinPos[1] + @DesktopHeight)) / 15)
				WinMove($hLockGUI, "", 0, $i)
				Sleep(10)
			Next
			WinMove($hLockGUI, "", 0, -@DesktopHeight)
			Exit
		Else
			For $i = $aWinPos[1] To 0 Step (@DesktopHeight - ($aWinPos[1] + @DesktopHeight)) / 25
				WinMove($hLockGUI, "", 0, $i)
				Sleep(10)
			Next
			WinSetOnTop($hLockGUI, "", 1)
			WinMove($hLockGUI, "", 0, 0)
		EndIf
		Return
	EndIf
	$aMousePos = MouseGetPos()
	If Not IsArray($CInfo) Or Not IsArray($aMousePos) Then Return
	WinMove($hLockGUI, "", 0, $aMousePos[1] - $CInfo[1])
EndFunc   ;==>_CDrag

Func WM_WINDOWPOSCHANGING($hWnd, $Msg, $wParam, $lParam)
	Local $iY_Min = -@DesktopHeight, $iY_Max = 0
	Local $stWinPos = DllStructCreate("uint;uint;int;int;int;int;uint", $lParam)

	Local $iLeft = DllStructGetData($stWinPos, 3)
	Local $iTop = DllStructGetData($stWinPos, 4)
	Local $iWidth = DllStructGetData($stWinPos, 5)
	Local $iHeight = DllStructGetData($stWinPos, 6)

	If $iTop < $iY_Min Then DllStructSetData($stWinPos, 4, $iY_Min)
	If $iTop > $iY_Max Then DllStructSetData($stWinPos, 4, $iY_Max)
EndFunc   ;==>WM_WINDOWPOSCHANGING
